/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Drawing;

using SdlDotNet.Graphics;
using Sdl = SdlDotNet.Graphics;
using SdlDotNet.Core;
using SdlDotNet.Input;

using BlindShark.GameElements;

using BlindShark.Widgets;

namespace BlindShark.GameStates
{
	/// <summary>
	/// Displays the players, and allows the selection of one of them
	/// </summary>
	public class GS_ProfileSelection : GameState
	{	
		#region Variables
		// Surfaces
		Surface _sfFrame;
		Surface _sfPlayerInfos;
				
		// Texts
		string _sFont;
		string _sTitle;
		Sdl.Font _fTitle;
		Sdl.Font _fPlayerName;
		Sdl.Font _fPlayerDescription;
		
		// Elements location
		Rectangle _rctFrame;
		Rectangle _rctTitle;
		Rectangle _rctPlayerInfos;
		
		// Players' list
		List<ProfileFile> _lsPlayers;
		ImageGallery _glPlayers;
		
		// Buttons
		Button _btnValidate;
		Button _btnBack;
		
		#endregion
		
		#region Constructors
		public GS_ProfileSelection()
		{
			_sFont = "fonts/Fertigo_17b.otf";
			
			// Initializing the font used for the Title
			_fTitle = new Sdl.Font(_sFont, 36);
			_fTitle.Underline = true;
			_sTitle = LanguageManager.Get("PSM_Title");
			
			// Initializing the Fonts used for the player informations
			_fPlayerName = new Sdl.Font(_sFont, 30);
			_fPlayerName.Underline = true;
			_fPlayerDescription = new Sdl.Font(_sFont, 20);
			
			
			// Initializing the buttons
			_btnValidate = new Button("gfx/buttons1.jpg", true);
			_btnValidate.Click += new Button.ClickEvent(Validate);
			_btnValidate.Font = _sFont;
			_btnValidate.TextColor = Color.Orange;
			_btnValidate.TextSize = 20;
			_btnValidate.Text = LanguageManager.Get("PSM_Validate");
			_btnBack = new Button("gfx/buttons1.jpg", true);
			_btnBack.Click += new Button.ClickEvent(GoBack);
			_btnBack.Font = _sFont;
			_btnBack.TextColor = Color.Orange;
			_btnBack.TextSize = 20;		
			_btnBack.Text = LanguageManager.Get("PSM_Back");
			
			// Initializing the Gallery
			_glPlayers = new ImageGallery();
			_glPlayers.BackColor = Color.Black;
			_glPlayers.BorderColor = Color.Gold;
			_glPlayers.BorderSize = 2;
			_glPlayers.Size = new Size(404, 204);
			_glPlayers.CenterImageSize = new Size(180, 180);
			_glPlayers.SideImageSize = new Size(90, 90);
			_glPlayers.SelectedIndexChanged += new ImageGallery.SelectedIndexChangedEvent(DisplayPlayers);
			
			// Initializing the list of players
			LoadPlayers();
			
			Keyboard.KeyRepeat = false;
		}
		#endregion
		
		#region GameState interface members
		public void Draw()
		{
			// Displaying the Frame
			ScreenManager.MainScreen.Blit(_sfFrame, _rctFrame.Location);
			
			//Displaying the Gallery
			ScreenManager.MainScreen.Blit(_glPlayers.Surface, _glPlayers.Location);
			
			// Displaying the selected player's infos
			ScreenManager.MainScreen.Blit(_sfPlayerInfos, _rctPlayerInfos.Location);
			
			// Displaying the buttons
			ScreenManager.MainScreen.Blit(_btnValidate.Surface, _btnValidate.Location);
			ScreenManager.MainScreen.Blit(_btnBack.Surface, _btnBack.Location);
		}
		
		public void CleanUp()
		{
			_sfFrame = null;
			_sfPlayerInfos = null;
			_btnValidate.CleanUp();
			_btnBack.CleanUp();
			_glPlayers.CleanUp();
			Events.Remove();
		}
		
		public void HandleEvents()
		{
			while(Events.Poll())
			{
				// Handling the buttons events
				_btnValidate.HandleEvents();
				_btnBack.HandleEvents();
				
				// Handling gallery events
				_glPlayers.HandleEvents();
			}
		}
		
		public void Update()
		{
			
		}
		
		public void Init()
		{
			// Displaying the background one for once
			ScreenManager.MainScreen.Blit(SurfaceManager.GetSurface("gfx/background.png"));
			
			// Initializing the frame
			_sfFrame = new Surface(SurfaceManager.GetSurface("gfx/background_infoWindow.png").Size);
			_sfFrame.Blit(SurfaceManager.GetSurface("gfx/background_infoWindow.png"));
			_rctFrame = new Rectangle(new Point(150,50),_sfFrame.Size);
			
			// Initializing the title location
			_rctTitle.Size = _fTitle.SizeText(_sTitle);
			_rctTitle.X = (_rctFrame.Width - _rctTitle.Width)/2;
			_rctTitle.Y = 15;
			
			// Displaying the title on the frame
			_sfFrame.Blit(_fTitle.Render(_sTitle, Color.OrangeRed), _rctTitle.Location);
			
			// Initializing the Gallery
			_glPlayers.Init();
			_glPlayers.X = _rctFrame.X + 50;
			_glPlayers.Y = _rctFrame.Y + _rctTitle.Bottom + 20;
			
			DisplayPlayers();
			
			// Initializing the buttons
			_btnValidate.Init();
			_btnValidate.X = _rctFrame.Right - 10 - _btnValidate.Width;
			_btnValidate.Y = _rctFrame.Bottom - 10 - _btnValidate.Height;
			
			_btnBack.Init();
			_btnBack.X = _rctFrame.Left + 10;
			_btnBack.Y = _rctFrame.Bottom - 10 - _btnBack.Height;
		}
		#endregion
		
		#region Display Functions
		private void DisplayPlayers()
		{			
			// Displaying the selected player's informations
			_rctPlayerInfos.Size = new Size(404, 140);
			_rctPlayerInfos.Y = _glPlayers.Y + _glPlayers.Height + 10;
			_rctPlayerInfos.X = _glPlayers.X + (_glPlayers.Width - _rctPlayerInfos.Width)/2;
			_sfPlayerInfos = new Surface(_rctPlayerInfos.Size);
			_sfPlayerInfos.Transparent = true;
			_sfPlayerInfos.Blit(_fPlayerName.Render(_lsPlayers[_glPlayers.SelectedIndex].profile.Name,Color.Orange),
			                    new Point((_rctPlayerInfos.Width-_fPlayerName.SizeText(_lsPlayers[_glPlayers.SelectedIndex].profile.Name).Width)/2,0));
			_sfPlayerInfos.Blit(_fPlayerDescription.Render(_lsPlayers[_glPlayers.SelectedIndex].profile.Description, 
			                                               Color.Orange, 
			                                               true, 
			                                               _rctPlayerInfos.Width - 10, 
			                                               4),
			                    new Point(5, _fPlayerName.SizeText(_lsPlayers[_glPlayers.SelectedIndex].profile.Name).Height + 5));
			
		}
		#endregion
	
		#region Buttons Events
		public virtual void Validate()
		{
			GameStateManager.ChangeState(new GS_PlayerProfile(_lsPlayers[_glPlayers.SelectedIndex].file));
		}
		
		public virtual void GoBack()
		{
			GameStateManager.ChangeState(new GS_MenuPlayers());
		}
		#endregion
		
		private void LoadPlayers()
		{
			// Clearing the list
			_lsPlayers = new List<ProfileFile>();
			
			// Checking the existence of the directory
			System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo("profiles");
			if (! directory.Exists)
				throw new Exception("No Profiles Directory");
			
			// Scanning the directory to create the profiles
			foreach(System.IO.FileInfo file in directory.GetFiles("*.xml"))
			{
				ProfileFile profileFile;
				profileFile.profile = new Profile();
				profileFile.profile.ReadFromXml(file.FullName);
				profileFile.file = file.Name;
				_lsPlayers.Add(profileFile);
			}
			
			// Sorting the list
			_lsPlayers.Sort();
			for(int i=0; i<_lsPlayers.Count; i++)
			{
				_glPlayers.Add("avatars/"+_lsPlayers[i].profile.PictureFile);
			}
		}
	}
}

